package cn.myzqu.start.lambda;

import cn.myzqu.start.lambda.entity.Student;

import java.util.*;
import java.util.stream.Collectors;

public class Test1 {


    public static void main(String[] args) {
        //线程
        //java8之前
        new Thread(new Runnable() {
            public void run() {
                System.out.println("hello world");
            }
        }).start();
        //java8之后
        new Thread(() -> System.out.println("hello world")).start();

        //遍历集合
        List<String> list = Arrays.asList("b", "c", "a");
        list.forEach(n -> System.out.println(n));

        //排序
        Collections.sort(list, String::compareTo);
        list.forEach(System.out::println);

        Student student1 = new Student("lida", 18);
        Student student2 = new Student("的川", 17);
        List<Student> studentList = new ArrayList<>();
        studentList.add(student1);
        studentList.add(student2);
        //排序
//        Collections.sort(studentList, Comparator.comparing(Student::getAge));
//        studentList.forEach(item -> System.out.println(item.getName() + ":" + item.getAge()));
        List<Student> studentList1 = studentList.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());
        studentList.forEach(item -> System.out.println(item.getName() + ":" + item.getAge()));
        studentList1.forEach(item -> System.out.println(item.getName() + ":" + item.getAge()));


        //Map<Integer, String> map = Stream.of("are","you","ok").collect(Collectors.toMap(s -> s.hashCode(), s -> s));


        //map函数可以说是函数式编程里最重要的一个方法了。map的作用是将一个对象变换为另外一个。
        List<Double> cost = Arrays.asList(10.0, 20.0, 30.0);
        cost.stream().map(x -> x + x * 0.5).forEach(System.out::println);

        //map的作用是将一个对象变为另外一个，而reduce实现的则是将所有值合并为一个。
        double allCost = cost.stream().map(x -> x + x * 0.05).reduce((sum, x) -> sum + x).get();
        System.out.println(allCost);

    }

}